package com.app.controller;


import com.app.mapper.AreasMapper;
import com.app.mapper.CitiesMapper;
import com.app.mapper.ProvincesMapper;
import com.app.mapper.ReturnHomeMapper;
import com.app.model.*;
import com.app.service.ReturnHomeService;
import com.app.util.Result;
import com.app.vo.ProvincesVo;
import com.app.vo.ReturnHomeVo;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;


@Controller
@RestController
@Data
public class ReturnHomeController {


    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    ProvincesMapper provincesMapper;
    @Autowired
    CitiesMapper citiesMapper;

    @Autowired
    AreasMapper areasMapper;
    @Autowired
    ReturnHomeMapper returnHomeMapper;
    @Autowired
    ReturnHomeService returnHomeService;


    @GetMapping("/getProvinceList")
    public Result<Object> getProvinceList() {
        List<ProvincesModel> provincesModelList = provincesMapper.selectList(null);
        List<CitiesModel> citiesModelList = citiesMapper.selectList(null);
        List<AreasModel> areasModelList = areasMapper.selectList(null);
        List<ProvincesVo> voList = new ArrayList<>();

        for (ProvincesModel province : provincesModelList) {//省级查询
            ProvincesVo vo = new ProvincesVo();
            vo.setCode(province.getProvinceid());
            vo.setData(province.getProvince());
            List<ProvincesVo> citiesList = new ArrayList<>();
            for (CitiesModel citiesModel : citiesModelList) {//市级查询
                if (Objects.equals(province.getProvinceid(), citiesModel.getProvinceid())) {
                    ProvincesVo citiesVo = new ProvincesVo();
                    citiesVo.setCode(citiesModel.getCityid());
                    citiesVo.setData(citiesModel.getCity());
                    citiesList.add(citiesVo);
                    List<ProvincesVo> areasList = new ArrayList<>();
                    for (AreasModel areasModel : areasModelList) {
                        if (areasModel.getCityid().equals(citiesModel.getCityid())) {
                            ProvincesVo areasVo = new ProvincesVo();
                            areasVo.setCode(areasModel.getAreaid());
                            areasVo.setData(areasModel.getArea());
                            areasList.add(areasVo);
                        }

                    }
                    citiesVo.setChildren(areasList);
                }

            }
            vo.setChildren(citiesList);
            voList.add(vo);

        }
        return Result.success(voList);
    }

    @PostMapping("/insertReturnHome")
    public Result<Object> insertReturnHome(ReturnHomeModel model
    ) {
        returnHomeMapper.insert(model);
        return Result.success("操作成功");
    }

    /**
     * @param page
     * @param limit
     * @param sex          性别
     * @param age          年龄
     * @param education    学历
     * @param domicile     户籍
     * @param workIndustry 务工行业
     * @return
     */
    @GetMapping("/getReturnHomeList")
    public Result getReturnHomeList(Integer page, Integer limit, String sex, String age, String education, String domicile, String workIndustry) {
        ReturnHomeVo returnHomeList = returnHomeService.getReturnHomePageList(page, limit, sex, age, education, domicile, workIndustry);
        return Result.success(returnHomeList);
    }

    /**
     * 用户信息导出excel
     *
     * @throws IOException
     * @return
     */
//    @RequestMapping("/exportReturnHome")
//    public Result<String> exportReturnHome(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        return Result.success("导出成功");
//    }


    /**
     *
     * @param response
     * @param model
     * @return
     * @throws Exception
     */
    @PostMapping("/exportReturnHome")
    @ResponseBody
    public void exportReturnHome(HttpServletResponse response, @RequestBody ReturnHomeModel model) throws Exception {
        List<ReturnHomeModel> returnHomeList = returnHomeService.getReturnHomeList(model.getSex(),model.getAge(), model.getEducation(), model.getDomicile(), model.getWorkIndustry());
        Map<String, Object> map=new HashMap<>();
        map.put("list",returnHomeList);
        ClassPathResource classPathResource = new ClassPathResource("C:/upload/moban.xlsx");
        String path = classPathResource.getPath();
        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
        String fileName = "问卷花名册" + System.currentTimeMillis() + ".xlsx";
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            response.flushBuffer();
            ServletOutputStream outputStream = response.getOutputStream();
            wb.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @PostMapping("/exportReturnHome2")
////    public Result<String> exportReturnHome(HttpServletResponse response,String sex, String age, String education, String domicile, String workIndustry) throws Exception {
//    public Result<String> exportReturnHome2(HttpServletResponse response,@RequestBody ReturnHomeModel model) throws Exception {
//        List<ReturnHomeModel> returnHomeList = returnHomeService.getReturnHomeList(model.getSex(),model.getAge(), model.getEducation(), model.getDomicile(), model.getWorkIndustry());
//        Map<String, Object> map=new HashMap();
//
//        map.put("list",returnHomeList);
//
//        ClassPathResource classPathResource = new ClassPathResource("C:/upload/moban.xlsx");
//        String path = classPathResource.getPath();
//        TemplateExportParams templateExportParams1 = new TemplateExportParams(path);
//        Workbook wb = ExcelExportUtil.exportExcel(templateExportParams1, map);
////        String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern(System.));
//        String fileName = "企业花名册" + System.currentTimeMillis() + ".xlsx";
//        try {
//            response.setContentType("application/vnd.ms-excel");
//            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
//            response.flushBuffer();
//            ServletOutputStream outputStream = response.getOutputStream();
//            wb.write(outputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return Result.success("导出成功");
//    }

}
